<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/8/28
 * Time: 下午4:52
 */

namespace App\WeChat\WxPay\Order;


class WxPayJsApiOrder extends WxPayOrder
{
    protected $values = [
        'appId'=>null,
        'timeStamp'=>null,
        'nonceStr'=>null,
        'package'=>null,
        'signType'=>'MD5',
        'paySign'=>null
    ];

    private $prepayId = null;

    /**
     * @param null $value
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function setAppid($value = null)
    {
        if (is_null($value)) {
            $this->values['appId'] = setting('wx_appid');
        } else {
            $this->values['appId'] = $value;
        }
    }

    /**
     * @return mixed
     */
    public function getAppid()
    {
        return $this->values['appId'];
    }

    /**
     * 设置支付时间戳
     * @param string $value
     **/
    public function setTimeStamp($value = null){
        if (is_null($value)) {
            $timestamp = time();
            $this->values['timeStamp'] = "$timestamp";
        } else {
            $this->values['timeStamp'] = $value;
        }
    }

    /**
     * 获取支付时间戳的值
     * @return string 值
     **/
    public function getTimeStamp(){
        return $this->values['timeStamp'];
    }

    /**
     * 随机字符串
     * @param string $value
     **/
    public function setNonceStr($value = null)
    {
        if (is_null($value)) {
            $this->values['nonceStr'] = md5(time().rand(100, 999));
        }else {
            $this->values['nonceStr'] = $value;
        }
    }
    /**
     * 获取notify随机字符串值
     * @return string 值
     **/
    public function getNonceStr()
    {
        return $this->values['nonceStr'];
    }


    /**
     * 设置订单详情扩展字符串
     * @param string $value
     **/
    public function setPackage($value)
    {
        $this->values['package'] = $value;
    }
    /**
     * 获取订单详情扩展字符串的值
     * @return string 值
     **/
    public function getPackage()
    {
        return $this->values['package'];
    }

    /**
     * 设置签名方式
     * @param string $value
     **/
    public function setSignType($value)
    {
        $this->values['signType'] = $value;
    }

    /**
     * 获取签名方式
     * @return string 值
     **/
    public function getSignType()
    {
        return $this->values['signType'];
    }

    /**
     * @return null
     */
    public function getPrepayId()
    {
        return $this->prepayId;
    }

    /**
     * @param null $prepayid
     */
    public function setPrepayId($prepayid): void
    {
        $this->prepayId = $prepayid;
        $this->setPackage("prepay_id=".$prepayid);
    }

    /**
     * @return array
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \Exception
     */
    public function getBizContent()
    {
        if (!$this->getAppId()) {
            $this->setAppid();
        }

        if (!$this->getTimeStamp()) {
            $this->setTimeStamp();
        }

        if (!$this->getNonceStr()) {
            $this->setNonceStr();
        }

        if (!$this->getPackage()) {
            throw new \Exception('缺少参数package', 1);
        }

        if (!$this->getSignType()) {
            throw new \Exception('缺少参数signType', 2);
        }

        $this->values['paySign'] = $this->makeSign();
        return $this->values;
    }
}
